package cn.wolfcode.trip.app.interceptor;

import cn.wolfcode.trip.app.controller.UserContext;
import cn.wolfcode.trip.base.domain.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//拦截器
public class LoginInterceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断是否有登录状态
        User currentUser = UserContext.getCurrentUser();

        if(currentUser == null){

            //重定向到登录页面
            response.sendRedirect("/login.html");
            //不放行
            return false;
        }

        return true;
    }
}
